package com.wzy.common.base.jwtToken;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * 2 * @Author: wuzhengyu
 * 3 * @Date: 2021/3/15 18:09
 * 4
 */
@Data
@Configuration
public class JwtUtil {

    //签名私钥
    private String KEY="NILIUSHIGUANG";
    //签名的失效时间
    private Long ttl=360000L;


    public String createjwt(String userName, Map<String,Object> map){
        //设置失效时间
        Long now=System.currentTimeMillis();
        long exp=ttl+now;
        //构建token
        JwtBuilder jwtBuilder =
                Jwts.builder().setSubject(userName).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256, KEY);
        jwtBuilder.setClaims(map);
        jwtBuilder.setExpiration(new Date(exp));
        String token = jwtBuilder.compact();
        //返回token
        return token;
    }

    /**
     * 解析token字符串
     */

    public Claims pasertToken(String token) throws Exception{
        Claims claims = Jwts.parser().setSigningKey(KEY).parseClaimsJws(token).getBody();

        return claims;
    }

}
